package com.day17_consumer;

import java.util.function.Consumer;

/**
 * @ClassName: ConsumerTest
 * @Author: zw
 * @Description: 出百万拳，行万里路；
 * @Create 2024/3/30-09:22
 * @Version 1.0
 */
public class ConsumerTest {
    public static void main(String[] args) {
        operatorString("hello", (String s) -> { // Lambda表达式
            System.out.println(s); // 操作行为
        });
        // Lambad 优化
        operatorString("hello", s -> System.out.println(s));
        // 方法引用的优化
        operatorString("hello", System.out::println);

        // Stringbuilder 反转字符
        operatorString("hello", s -> {
            System.out.println(new StringBuilder(s).reverse().toString());
        });
        // 反转字符的Lambda 优化
        operatorString("hello", s -> System.out.println(new StringBuilder(s).reverse().toString()));
        //
        System.out.println("一次消费两次同一个字符串数据");
        // s-> System.out.println(s) 第一次打印
        // s -> System.out.println(new StringBuilder(s).reverse().toString())
        // 第二次西安反转再打印
        operatorString("赵旺", s-> System.out.println(s), s -> System.out.println(new StringBuilder(s).reverse().toString()));
        // AddThen
        operatorStringAddThen("赵旺", s-> System.out.println(s), s -> System.out.println(new StringBuilder(s).reverse().toString()));
    }
    // 定义一个方法，消费字符串数据
    private static void operatorString(String name, Consumer<String> con) {
        con.accept(name);
    }

    // 用不同的方式消费同一个字符串数据两次
    private static void operatorString(String name, Consumer<String> con1, Consumer<String> con2) {
        con1.accept(name);
        con2.accept(name);
    }
    // 调用addThen方式 达到消费同一个字符串数据两次
    private static void operatorStringAddThen(String name, Consumer<String> con1, Consumer<String> con2) {
        con1.andThen(con2).accept(name); // 先通过con1.消费name，然后通过con2消费name
    }
}
